<template>
  <div class="edit-container">
    <van-nav-bar
      title="编辑资料"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="auavr">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <div class="cells">
      <van-cell-group>
        <van-cell
          title="名称"
          :value="userInfo.name"
          is-link
          @click="editerClick"
        />
        <van-cell
          title="性别"
          :value="userInfo.gender ? '女' : '男'"
          @click="showPopup = true"
          is-link
        />
        <van-cell
          title="生日"
          :value="userInfo.birthday"
          is-link
          @click="
            showDatePicker = true
            currentDate = new Date(userInfo.birthday)
          "
        />
      </van-cell-group>
    </div>
    <div class="editer">
      <van-dialog
        v-model="showDiog"
        title="用户名"
        show-cancel-button
        @confirm="updateName"
      >
        <van-field ref="input" v-model="newName" placeholder="请输入用户名" />
      </van-dialog>
    </div>
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar title="性别" left-arrow @click-left="showPopup = false" />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="updateBirthday"
        @cancel="showDatePicker = false"
      />
    </van-popup>
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="updatePhoto">确认</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo, editUserPhoto } from './user.js'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  components: {
    VueCropper
  },
  data () {
    return {
      showDiog: false,
      newName: '',
      showPopup: false,
      showDatePicker: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      img: '',
      showCropper: false
    }
  },
  methods: {
    editerClick () {
      this.showDiog = true
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.input.focus()
      }, 100)
    },
    updateName () {
      editUserInfo({ name: this.newName }).then(res => {
        this.$toast.success('更新成功')
        this.$store.commit('editUserInfo', {
          ...this.userInfo,
          name: this.newName
        })
      })
    },
    editGender (gender) {
      this.showPopup = false
      editUserInfo(gender).then(res => {
        this.$toast.success('更新成功')
        this.$store.commit('editUserInfo', {
          ...this.userInfo,
          gender
        })
      })
    },
    updateBirthday () {
      let birthday = moment(this.currentDate).format('YYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.$toast.success('更新成功')
        this.$store.commit('editUserInfo', {
          ...this.userInfo,
          birthday: birthday
        })
        this.showDatePicker = false
      })
    },
    updatePhoto () {
      this.$refs.cropper.getCropBlob(data => {
        let fd = new FormData()
        fd.append('photo', data)
        editUserPhoto(fd).then(res => {
          this.$toast.success('提示成功')
          this.showCropper = false
          this.$store.commit('editUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
        })
      })
    },
    afterRead (file) {
      this.img = file.content
      this.showCropper = true
      /*  */
    }
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less">
.edit-container {
  .van-nav-bar__content {
    background-color: #009afb;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-nav-bar__left {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
    .van-nav-bar__arrow {
      color: #fff;
    }
  }
  .auavr {
    display: flex;
    height: 150px;
    justify-content: center;
    align-items: center;
  }
  .van-uploader {
    position: absolute;
    width: 120px;
    height: 120px;
    opacity: 0;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
  .cropper {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
